package com.gdk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.HotelService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.service.PromotionService;
import com.gdk.service.ShopService;
import com.gdk.utils.HUtil;
import com.gdk.utils.XVDateUtils;

/**
 * 酒店controller
 * */

@Controller
public class HotelController {
	
	@Autowired
    private HotelService hotelService;
	
	
	/**
	 * 查询出所有酒店
	 * */
	@RequestMapping("/preAddHotelRoom.do")
	public String preAddHotelRoom(@RequestParam Map<String,Object> map, HttpServletRequest request) {
	
		
		List<Map<String, Object>> allHotel = hotelService.selectAllHotel(HUtil.putInMap("hrn_status",0));
		
		request.setAttribute("allHotel", allHotel);
		return "jsp/hotel/hotel-room-add";
	}
	
	
	
	/**
	 * 新增酒店房间
	 * */
	@ResponseBody
	@RequestMapping("/addHotelRoom.do")
	public int addHotelRoom(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("hr_status", 0);
		map.put("hr_addtime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("酒店房间插入  :  "+map);
		
		int re_status = hotelService.addHotelRoom(map);
		System.out.println("结果 : "+re_status);
		if(re_status == 1) {
			//创建床位
			Map<String,Object> bedMap = new HashMap<String,Object>();
			//bedMap.put("hb_bed", map.get("hr_room").toString()+" - 01");
			bedMap.put("hb_bed", "01");
			bedMap.put("hr_id", map.get("hr_id"));
			bedMap.put("hb_status", 0);
			bedMap.put("hb_addtime", XVDateUtils.getCurrenTimeMillis());
			
			hotelService.addHotelBed(bedMap);
			
			//bedMap.put("hb_bed", map.get("hr_room").toString()+" - 02");
			bedMap.put("hb_bed", "02");
			bedMap.put("hr_id", map.get("hr_id"));
			bedMap.put("hb_status", 0);
			bedMap.put("hb_addtime", XVDateUtils.getCurrenTimeMillis());
			
			hotelService.addHotelBed(bedMap);
		}
		
		
		return re_status;
	}
	
	/**
	 * 查询所有房间
	 * */
	@RequestMapping("/selectAllHotelRoom.do")
	public String selectAllHotelRoom(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);
		
	
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allHotelRoom = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = hotelService.selectAllHotelRoom(map).size();
		
		map.put("allHotelRoomItem",itemCount);
		
		
		
		
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/hotel/hotel-room-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allHotelRoom = hotelService.selectAllHotelRoom(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allHotelRoom) {
			if(map2.get("hr_addtime") != null) {
				int p = Integer.valueOf(map2.get("hr_addtime").toString());
				if(p != 0) {
					map2.put("hr_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		List<Map<String, Object>> allBedBindUser = hotelService.selectAllBedBindUser(map);
		
		request.setAttribute("allBedBindUser",allBedBindUser);
		/*List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		request.setAttribute("allHotelRoom",allHotelRoom);
		return "jsp/hotel/hotel-room-list";
	}
	
	
	
	/**
	 *修改前查询酒店房间详情
	 * */
	@RequestMapping("/preSelectHotelRoomDetails.do")
	public String preSelectHotelRoomDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> hotelRoomDetails = hotelService.selectHotelRoomDetails(map);
		
		request.setAttribute("hotelRoomDetails",hotelRoomDetails);
		
		List<Map<String, Object>> allHotel = hotelService.selectAllHotel(HUtil.putInMap("hrn_status",0));
		
		request.setAttribute("allHotel", allHotel);

		return "jsp/hotel/hotel-room-edit";
	}
	
	/**
	 * 修改酒店房间
	 * */
	@ResponseBody
	@RequestMapping("/updateHotelRoom.do")
	public int updateHotelRoom(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("酒店房间 : "+map);
		int shop = hotelService.updateHotelRoomDetails(map);
		return shop;
	}

	
	
	/**
	 * 新增酒店
	 * */
	@ResponseBody
	@RequestMapping("/addHotel.do")
	public int addHotel(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("hrn_status", 0);
		map.put("hrn_addtime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("酒店插入  :  "+map);
		int re_status = hotelService.addHotel(map);
		return re_status;
	}
	/**
	 * 查询所有酒店
	 * */
	@RequestMapping("/selectAllHotel.do")
	public String selectAllHotel(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("hrn_status", 0);
		System.out.println("查询  :  "+map);
		
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allHotel = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = hotelService.selectAllHotel(map).size();
		
		map.put("allHotelItem",itemCount);
		
		
		
		
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/hotel/hotel-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allHotel = hotelService.selectAllHotel(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allHotel) {
			if(map2.get("hrn_addtime") != null) {
				int p = Integer.valueOf(map2.get("hrn_addtime").toString());
				if(p != 0) {
					map2.put("hrn_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}

		/*List<Map<String, Object>> allModule = navigationService.selectAllModule();
		request.setAttribute("allModule", allModule);*/
		request.setAttribute("allHotel",allHotel);
		return "jsp/hotel/hotel-list";
	}
	/**
	 *修改前查询酒店详情
	 * */
	@RequestMapping("/preSelectHotelDetails.do")
	public String preSelectHotelDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> hotelDetails = hotelService.selectHotelDetails(map);
		
		request.setAttribute("hotelDetails",hotelDetails);
		

		return "jsp/hotel/hotel-edit";
	}
	
	/**
	 * 修改酒店
	 * */
	@ResponseBody
	@RequestMapping("/updateHotel.do")
	public int updateHotel(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("酒店 : "+map);
		int shop = hotelService.updateHotelDetails(map);
		return shop;
	}

	/**
	 * 删除酒店房间
	 * */
	@ResponseBody
	@RequestMapping("/delHotelRoom.do")
	public int delHotelRoom(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("删除酒店房间  : "+map);
		//删除酒店房间表
		int roomByHR = hotelService.delHotelRoomByHR(map);
		//删除酒店床位表
		hotelService.delHotelBedByHR(map);
		//删除酒店关联表
		hotelService.delBindByHR(map);
		
		return roomByHR;
	}
	
	/**
	 * 删除酒店
	 * */
	@ResponseBody
	@RequestMapping("/delHotel.do")
	public int delHote(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("删除酒店  : "+map);
		//查询出所以酒店
		List<Map<String, Object>> allHotelBindRoom = hotelService.selectHotelBindRoom(map);
		for (Map<String, Object> map2 : allHotelBindRoom) {
			//删除酒店床位表
			hotelService.delHotelBedByHR(map2);
			//删除酒店关联表
			hotelService.delBindByHR(map2);
			//删除酒店房间表
			hotelService.delHotelRoomByHR(map2);
		}
		//删除酒店
		int hotel = hotelService.delHotel(map);
		
		return hotel;
	}
}